/*初始化函数*/
function init() {
    let layer = layui.layer, form = layui.form;
    let init_load_index = layer.load(1);
    // 0.1 加载分类
    $.get("/typeInfo", {"token": localStorage.getItem("token")}, function (result) {
        if (result.code === 200) {
            let html_str = '<option value="">请选择</option>';
            $.each(result.data, function (i, n) {
                html_str += '<option value="' + n.id + '">' + n.name + '</option>';
            })
            $("select[name='search_type_id']").html(html_str);
            form.render("select");//渲染select框

            // 0.2 点击查询按钮
            $("input[name='search_title']").val("");
            $("input[name='search_username']").val("");
            $("select[name='search_type_id']").val("");
            $("#search_btn").click();//点击查询按钮
        } else if (result.code === 401) window.parent.location.href = "/login.html";
        else layer.msg(result.msg, {icon: 5});
    })
}

layui.use(['layer', 'form'], function () {
    let layer = layui.layer, form = layui.form;

    // 1.查询按钮
    $("#search_btn").click(function () {
        layer.load(1);
        let title = $.trim($("input[name='search_title']").val());
        let username = $.trim($("input[name='search_username']").val());
        let type_id = $("select[name='search_type_id']").val();
        //let published =true;//对于在回收站中而言，这个参数本来不必要传，但是后台要求强制传输
        let flag = 1;//回收站必须传入此参数，且为1
        $.ajax({
            url: "/blog/info",
            method: "GET",
            data: {
                "token": localStorage.getItem("token"),
                "title": title,
                "username": username,
                "type_id": type_id,
                "flag": flag
            },
            dataType: "JSON",
            traditional: true,//开启数组参数传递
            success: function (result) {
                if (result.code === 200) {
                    let html_str = "";
                    $.each(result.data, function (i, n) {
                        html_str += '<tr><td><input type="radio" name="blog_id" value="' + n.id + '"></td>';
                        html_str += '<td>' + n.title + '</td>';
                        html_str += '<td>' + n.createUser.username + '</td>';
                        html_str += '<td>' + n.typeName + '</td>';
                        html_str += '<td>' + n.recommendLevel + '</td>';
                        html_str += '<td>' + n.createFlag + '</td>';
                        html_str += '<td>' + n.published + '</td>';
                        html_str += '<td>' + n.editTime + '</td></tr>';
                    })
                    $("#blog_list").html(html_str);
                } else layer.msg(result.msg, {icon: 5});
                layer.closeAll("loading");
            }
        })
    })

    // 2.恢复按钮
    $("#edit_btn").click(function () {
        let $blog_checked = $("input[type='radio'][name='blog_id']:checked");
        if ($blog_checked.length !== 1) {
            layer.msg("请选择需要恢复的博客", {icon: 5});
            return false;
        }
        layer.confirm("确认要恢复博客：" + $blog_checked.parent().next().html(),
            {icon: 3, title: "警告"}, function (index) {
                $.ajax({
                    url: "/blog/recycle",
                    method: "put",
                    data: {
                        "token": localStorage.getItem("token"),
                        "id": $blog_checked.val()
                    }, dataType: "json",
                    success: function (result) {
                        if (result.code === 200) {
                            $blog_checked.parent().parent().remove();
                            layer.close(index);
                            layer.msg(result.msg, {icon: 1});
                        } else layer.msg(result.msg);
                    }
                })
            })
    })

    // 开始初始化
    init();

})
